package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import Models.MashStep;

public class MashStepPresenter extends AbstractPresenter<MashStep> {
	final String SINGLEITEM = "mashStep";
	final String MULTIITEM = "mashSteps";
	
	public ArrayList<MashStep> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public MashStep tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public MashStep createItemFromJSONObject(JSONObject jsonObject){
		MashStep item = new MashStep();
		try{
			item.setId(jsonObject.getInt("stepID"));
			item.setMashType(jsonObject.getInt("mashType"));
			item.setMinutes(jsonObject.getInt("minutes"));
			item.setRatio(jsonObject.getDouble("ratio"));
			item.setTemperature(jsonObject.getDouble("temperature"));
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(MashStep item){
		try{
			JSONObject json = new JSONObject();
			json.put("stepID", item.getId());
			json.put("mashType", item.getMashType());
			json.put("minutes", item.getMinutes());
			json.put("ratio", item.getRatio());
			json.put("temperature", item.getTemperature());
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public MashStep getEmptyEntity(){
		MashStep entity = new MashStep();
		return entity;
	}
}
